package br.com.canvasForAll.model.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.Query;

import br.com.canvasForAll.model.entity.Comentario;
import br.com.canvasForAll.model.entity.Postit;
import br.com.canvasForAll.model.generic.dao.GenericCrudDao;
import br.com.canvasForAll.model.repository.ComentarioRepository;

@Stateless
@Named("comentarioRepository")
public class ComentarioDao extends GenericCrudDao<Comentario> implements ComentarioRepository {
	
	public ComentarioDao() {
		super(Comentario.class);
	}

	@Override
	public Comentario obterId(Long entityId) throws Exception {
		return super.obterPorId(entityId);
	}

	@Override
	public List<Comentario> obterExemplo(String namedQuery, Map<String, Object> parametros) throws Exception {
		return super.obterPorExemplo(namedQuery, parametros);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Comentario> obterComentarioPorPostit(Postit postit) throws Exception {
		String sql = "SELECT c FROM Comentario c WHERE c.postit =:postit";
		
		Query query = em.createQuery(sql);
		query.setParameter("postit", postit);
		
		Collection<Comentario> comentarios = query.getResultList();
		
		if(comentarios == null){
			comentarios = new ArrayList<Comentario>();
		}
		
		return comentarios;
	}
	
	

}
